package com.ruoyi.web.controller.mall.shopExtract.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.mall.shopExtract.domain.ShopExtract;
import com.ruoyi.web.controller.mall.shopExtract.service.IShopExtractService;
import com.ruoyi.web.controller.shop.shop.domain.ShopShop;
import com.ruoyi.web.controller.shop.shop.service.IShopShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商家提现记录Controller
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mall/shopExtract")
public class ShopExtractController extends BaseController
{

    @Autowired
    private IShopExtractService shopExtractService;
    @Autowired
    private IShopShopService shopShopService;

    /**
     * 查询商家提现记录列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ShopExtract shopExtract)
    {
        startPage();
        List<ShopExtract> list = shopExtractService.selectShopExtractList(shopExtract);
        return getDataTable(list);
    }

    /**
     * 商家提现记录详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        ShopExtract shopExtract = shopExtractService.selectShopExtractById(id);
        return AjaxResult.success(shopExtract);
    }

    /**
     * 商家提现记录详情
     */
    @PostMapping(value = "/getByType")
    public AjaxResult getByType(@RequestBody Map<String,Object> map)
    {
        ShopExtract shopExtract = shopExtractService.getByType(map);

        return AjaxResult.success(shopExtract);
    }

    /**
     * 获取平台电话
     */
    @PostMapping(value = "/platPhone")
    public AjaxResult platPhone()
    {
        Map<String,Object> map = shopExtractService.platPhone();
        return AjaxResult.success(map);
    }

    /**
     * 新增保存商家提现记录
     */
    @Log(title = "商家提现记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody ShopExtract shopExtract)
    {
        ShopShop shopShop = shopShopService.selectShopShopById(shopExtract.getShopId());
        if (shopShop==null){
            return AjaxResult.error("店铺信息异常");
        }
        if (shopExtract.getMoney().compareTo(new BigDecimal("0"))<1){
            return AjaxResult.error("提现金额必须大于0");
        }
        if (shopExtract.getMoney().compareTo(shopShop.getMoney())>0){
            return AjaxResult.error("提现金额必须小于店铺余额");
        }
        shopExtract.setStatus(0);
        shopExtract.setId(StringUtils.get32UUID());
        shopExtract.setCreateAt(DateUtils.getNowDate());
        shopExtract.setDelFlag(0);
        return toAjax(shopExtractService.insertShopExtract(shopExtract));
    }

    /**
     * 导出商家提现记录列表
     */
    @Log(title = "商家提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(ShopExtract shopExtract)
    {
        List<ShopExtract> list = shopExtractService.selectShopExtractList(shopExtract);
        ExcelUtil<ShopExtract> util = new ExcelUtil<ShopExtract>(ShopExtract.class);
        return util.exportExcel(list, "shopExtract");
    }

    /**
     * 修改保存商家提现记录
     */
//    @Log(title = "商家提现记录", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    public AjaxResult editSave(ShopExtract shopExtract)
//    {
//        return toAjax(shopExtractService.updateShopExtract(shopExtract));
//    }

    /**
     * 删除商家提现记录
     */
//    @Log(title = "商家提现记录", businessType = BusinessType.DELETE)
//    @PostMapping( "/remove")
//    public AjaxResult remove(String[] ids)
//    {
//        return toAjax(shopExtractService.deleteShopExtractByIds(ids));
//    }
}
